package com.jacky.util;

import android.content.Context;
import android.graphics.Rect;
import android.hardware.Camera;
import android.os.Handler;
import android.os.Message;
import android.util.Size;

import com.yunmai.cc.idcard.controler.OcrConstant;
import com.yunmai.cc.idcard.controler.OcrManager;
import com.yunmai.cc.idcard.vo.IdCardInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;

/**
 * ocr识别管理
 */
/* package */class OcrRectManager32 extends OcrRectManager {

    private final String TAG = "OcrRectManager";
    private final Handler mHandler;
    private OcrManager mOcrManager;
    private final Context mContext;
    private Rect mUsedRect;
    private int mPreviewWidth;
    private int mPreviewHeight;

    public OcrRectManager32(Context context, Handler handler){
        mContext = context;
        mHandler = handler;
        mOcrManager = new OcrManager(mHandler, mContext);
    }

    public boolean isNeedUpdatePreviewSize(){
        return mPreviewHeight == 0 || mPreviewWidth == 0;
    }

    public void setOcrRect(int previewWidth, int previewHeight, Rect rect){
        mPreviewWidth = previewWidth;
        mPreviewHeight = previewHeight;
        if(rect != null && rect.width() > 0){
            if(rect.top < 0) rect.top = 0;
            if(rect.bottom < 0) rect.bottom = 0;
            if(rect.right < 0) rect.right = 0;
            if(rect.left < 0) rect.left = 0;
            mUsedRect = rect;
        }
        OcrLogger.d("ocr rect,left,top-right,bottom",mUsedRect, "preview w*h",previewWidth,previewHeight);
    }

    /**
     * ocr识别摄像头
     */
    public void ocrRectData(byte[] data){
        if (mOcrManager == null) {
            mOcrManager = new OcrManager(mHandler, mContext);
        }
        if (data != null && data.length > 0) {
            OcrLogger.d("ocr preivew w*h",mPreviewWidth,mPreviewHeight,mUsedRect);
            if(mUsedRect == null){
                mUsedRect = new Rect(0,0,mPreviewWidth,mPreviewHeight);
                OcrLogger.d("ocr mUsedRect null,set to default",mUsedRect);
            }
            //得到需要识别的数据，进行识别
           try{
               mOcrManager.recognBC(data, mPreviewWidth, mPreviewHeight, mUsedRect);
           }catch (Exception e){
               e.printStackTrace();
           }
        }
    }

    public void ocrRectData(byte[] data,int width,int height){
        if (mOcrManager == null) {
            mOcrManager = new OcrManager(mHandler, mContext);
        }
        if (data != null && data.length > 0) {
            OcrLogger.d("ocr preivew w*h",mPreviewWidth,mPreviewHeight,mUsedRect,"width",width,height);
            //得到需要识别的数据，进行识别
            mOcrManager.recognBC(data, width, height, mUsedRect);
        }
    }

    boolean can = false;
    int count = 0;
    //仅仅用于测试
    public void testForSaveImage(byte[] data_p){
        if(count++ / 8 == 0) can = true;
        if(can){
            can = false;
            FilePathUtils.savePreviewDataToImage(data_p, mPreviewWidth
                    , mPreviewHeight, mUsedRect,null,null);
        }
    }

    /**
     * 获取识别数据
     * @param imgFile 整个识别的图片保存路径
     * @param headFile 识别出来的头像图片保存路径
     * @return 识别内容
     */
    public String getRectResult(String imgFile,String headFile){
        if(mOcrManager == null) {
            OcrLogger.d("ocrManager is null,can use it's method getResult");
            return null;
        }
        IdCardInfo idCardInfo = mOcrManager.getResult(imgFile, headFile);
        try {
            String card = new String(idCardInfo.getCharInfo(), "gbk");
            //card最后一个"}"后面是乱码，导致解析不出来
            return card.substring(0, card.lastIndexOf("}") + 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    private final Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {
        public void onPreviewFrame(byte[] data, Camera camera) {
            sendPreviewDataToOcr(data);
        }
    };

    /**
     * 将一帧数据发送给 主线程处理
     * @param data 一帧预览画面数据
     */
    public void sendPreviewDataToOcr(byte[] data){
        Message msg = mHandler.obtainMessage();
        msg.what = OcrConstant.TAKE_PREVIEW_DATA_OK;
        msg.obj = data;
        mHandler.sendMessage(msg);
    }

    /**
     * 获取摄像头单帧数据
     */
    public Camera.PreviewCallback getPreviewCallback(){
        return previewCallback;
    }

}
